﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using BCCommon;
using BCEntity.WeChatComponent.WeChatUserTag;
using Dapper;

namespace BCData.WeChatComponent.WeChatUserTag
{
    /// <summary>
    /// 微信用户标签数据处理
    /// </summary>
    public class WeChatUserTagData : IWeChatUserTagData
    {
        private readonly IDatabaseContext databaseContext;
        
        public WeChatUserTagData(IDatabaseContext databaseContext)
        {
            this.databaseContext=databaseContext;
        }

        public bool Add(WeChatUserTagEntity weChatUserTagEntity)
        {
            string sql = @"INSERT INTO WeChatUserTag(WeChatUserTagId,Name)VALUES(?WeChatUserTagId,?Name);";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WeChatUserTagId", weChatUserTagEntity.WeChatUserTagId, DbType.Int32,size:11);
            parameters.Add("?Name", weChatUserTagEntity.Name, DbType.AnsiString, size: 30);
            int result = this.databaseContext.Master.Execute(sql, parameters, databaseContext.Transaction);
            return result > 0;
        }

        public bool Delete(int tagId)
        {
            string sql = @"DELETE FROM WeChatUserTag WHERE WeChatUserTagId=?WeChatUserTagId;";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WeChatUserTagId", tagId, DbType.Int32, size: 11);
            int result = this.databaseContext.Master.Execute(sql, parameters, databaseContext.Transaction);
            return result > 0;
        }

        public bool Delete()
        {
            string sql = @"DELETE FROM WeChatUserTag;";
            int result = this.databaseContext.Master.Execute(sql,null, databaseContext.Transaction);
            return result > 0;
        }

        public IEnumerable<WeChatUserTagEntity> Get()
        {
            string sql = @"SELECT WeChatUserTagId,Name,CreateTime FROM WeChatUserTag;";
            return this.databaseContext.Slave.Query<WeChatUserTagEntity>(sql);
        }

        public bool Update(WeChatUserTagEntity weChatUserTagEntity)
        {
            string sql = @"UPDATE WeChatUserTag SET
                                Name=?Name
                            WHERE WeChatUserTagId=?WeChatUserTagId;";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WeChatUserTagId", weChatUserTagEntity.WeChatUserTagId, DbType.Int32, size: 11);
            parameters.Add("?Name", weChatUserTagEntity.Name, DbType.AnsiString, size: 30);
            int result = this.databaseContext.Master.Execute(sql, parameters,databaseContext.Transaction);
            return result > 0;
        }
    }
}
